package top.lshaci.learning.springboot.mybatisplus;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import freemarker.template.TemplateException;
import top.lshaci.learning.springboot.mybatisplus.baseentity.MpTenantEntity;

import java.io.IOException;
import java.util.Collections;


/**
 * Project code generator
 *
 * @author lshaci
 * @since 1.0.1
 */
public class CodeGenerator {

    /**
     * Project package
     */
    private static String projectPackage = "top.lshaci.learning.springboot";

    /**
     * Database url
     */
    private static String url = "jdbc:mysql://127.0.0.1:3306/mybatisplus?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull";
    /**
     * Database username
     */
    private static String username = "root";
    /**
     * Database password
     */
    private static String password = "root";

    /**
     * main method, execute code generator
     */
    public static void main(String[] args) throws IOException, TemplateException {
        String sub = CodeGenerator.class.getResource("/").getPath().substring(1);
        String projectPath = StrUtil.subBefore(sub, "target", true);

        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author("lshaci") // 设置作者
                            .disableOpenDir()
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(projectPath + "src/main/java") // 指定输出目录
                    ;
                })
                .packageConfig(builder -> {
                    builder.parent(projectPackage) // 设置父包名
                            .moduleName("mybatisplus") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, projectPath + "src/main/resources/mapper/")) // 设置mapperXml生成路径
                    ;
                })
                .templateConfig(builder -> {
                    builder.service(ConstVal.TEMPLATE_SERVICE);
                    builder.controller(ConstVal.TEMPLATE_CONTROLLER);
                })
                .strategyConfig(builder -> {
                    builder.addInclude("sys_simple_sn") // 设置需要生成的表名
                            .addTablePrefix("sys_", "sys_dp_") // 设置过滤表前缀
                            .entityBuilder()
                            .enableFileOverride()
//                            .formatFileName("%sEntity")
                            .superClass(MpTenantEntity.class)
                            .columnNaming(NamingStrategy.underline_to_camel)
                            .enableChainModel()
                            .enableLombok()
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .controllerBuilder()
                            .enableRestStyle()
                    ;
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();

    }

}
